package com.sztx.pay.center.core.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.Constance.ActState;
import com.sztx.pay.center.common.util.Constance.FreezedOperator;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.Constance.UserType;
import com.sztx.pay.center.common.util.StringUtil;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.BillnoService;
import com.sztx.pay.center.core.service.bean.BatchActTransferReq;
import com.sztx.pay.center.core.service.bean.BatchActTransferRsp;
import com.sztx.pay.center.core.service.bean.CreateActRep;
import com.sztx.pay.center.core.service.bean.CreateActReq;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFreezeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserActFreezeDO;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.common.util.secret.MD5Util;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.FilterRule;

@Service
public class UserActServiceImpl extends BaseServiceImpl implements ActService {
	
	@Autowired
	private UserActMysqlDAO userActMysqlDAO;

	@Autowired
	private UserActFlowMysqlDAO userActFlowMysqlDAO;
	
	@Autowired
	private UserActFreezeMysqlDAO userActFreezeMysqlDAO;	
	
	@Autowired
	private BillnoService billnoService;
	
	@Override
	@Transactional
	public List<CreateActRep> createUserAct(String userId, int userType) {		
		List<CreateActRep> actRep = new ArrayList<CreateActRep>();;
		ValidateUtil.isNotBlank(userId, "userId不能为空");			
		List<UserType> uType = UserType.listOf(userType);
		ValidateUtil.notNull(uType, "userType参数不合法");
		
		List<UserActDO> userActList = userActMysqlDAO.listByUserId(userId);		
		List<UserActDO> addUserActList = new ArrayList<UserActDO>();

		UserActType[]  userActTypes = UserActType.values();
		for (UserActType userActType : userActTypes) {						
			int actUType = userActType.getuType();			
			if ((actUType & userType) == 0) {//用户没有创建此账号权限
				continue;
			}
			
			boolean isNeedTradeAct = true;			
			if (userActList != null && !userActList.isEmpty()) {								
				for (UserActDO act: userActList) {					
					if (act.getActType().intValue() == userActType.getCode()) { //账户已存在						
						isNeedTradeAct = false;
						continue;
					}
				}
			}

			if (isNeedTradeAct) {			
				UserActDO userAct = initUserAct(userId, userActType, "", "");			
				addUserActList.add(userAct);
			}				
		}		
		
		if (userActList != null && !userActList.isEmpty()) {
			for (UserActDO act: userActList) {					
				actRep.add(new CreateActRep(act.getUserId(), act.getActType(), act.getActNo()));
			}
		}		
		
		if (!addUserActList.isEmpty()) {
			userActMysqlDAO.batchSave(addUserActList);			
			for (UserActDO userActDO : addUserActList) {
				actRep.add(new CreateActRep(userActDO.getUserId(), userActDO.getActType(), userActDO.getActNo()));
			}
		}
		
		return actRep;
	}

	@Override
	@Transactional
	public String createAct(String userId, int userType, int actType) {		
		
		ValidateUtil.isNotBlank(userId, "userId不能为空");			
		List<UserType> uType = UserType.listOf(userType);
		ValidateUtil.notNull(uType, "userType参数不合法");
		UserActType userActType = UserActType.valueOf(actType);
		ValidateUtil.notNull(userActType, "actType参数不合法");
		
		if ((userActType.getuType() & userType) == 0) {
			throw new BusinessException("用户["+userId+"]没有权限创建"+userActType.getDesc());
		}
		
		UserActDO userActDO = userActMysqlDAO.findByUserId(userId, actType);
		if (userActDO !=null) {
			return userActDO.getActNo();
		}
		
		UserActDO userAct = initUserAct(userId, userActType, "", "");		
		userActMysqlDAO.save(userAct);		
		
		return userAct.getActNo();
	}

	@Override
	@Transactional
	public List<CreateActRep> createUserAct(List<CreateActReq> actReq) {	
		List<CreateActRep> actRep = null;
		
		ValidateUtil.notEmpty(actReq, "请求参数不能为空");		
		Set<UserActDO> userSet = new HashSet<UserActDO>();		
		Set<String> userIdSet = new HashSet<String>();
		
		for (CreateActReq req : actReq) {			
			String userId = req.getUserId();
			int actType = req.getActType();
			
			String innerRemark = req.getInnerRemark();
			String userRemark = req.getUserRemark();
			
			UserActType aType = UserActType.valueOf(actType);			
			if (aType == null) {
				LOG.error("用户"+userId+"的actType="+actType+"参数异常");
				throw new BusinessException("actType参数不合法");
			}
			
			if (StringUtil.isNotBlank(innerRemark)) {
				ValidateUtil.strMaxLen(innerRemark, 255, "innerRemark长度不能超过255个字符");
			}
			
			if (StringUtil.isNotBlank(userRemark)) {
				ValidateUtil.strMaxLen(userRemark, 255, "userRemark长度不能超过255个字符");
			}
			
			userIdSet.add(userId);
			userSet.add(initUserAct(userId, aType, innerRemark, userRemark));
		}		
		
		List<UserActDO> userActList = findByUserList(new ArrayList<String>(userIdSet));
		
		if (userActList == null || userActList.isEmpty()) {
			userActMysqlDAO.batchSave( new ArrayList<UserActDO>(userSet));			
		}else {
			for (UserActDO uAct : userActList) {			
				userSet.remove(uAct);
			}
			
			if (!userSet.isEmpty()) {
				userActMysqlDAO.batchSave( new ArrayList<UserActDO>(userSet));
			}	
		}
		
		if (!userSet.isEmpty()) {
			actRep = new ArrayList<CreateActRep>();
			for (UserActDO userActDO : userSet) {
				actRep.add(new CreateActRep(userActDO.getUserId(), userActDO.getActType(), userActDO.getActNo()));
			}
		}
		
		return actRep;
	}

	@Override
	@Transactional
	public void closeAct(String actNo, String operatorUser, String attach) {
		ValidateUtil.isNotBlank(operatorUser, "operatorUser不能为空");
		
		ValidateUtil.isNotBlank(actNo, "actNo不能为空");
		
//		UserActDO userAct = userActMysqlDAO.findByActNo(actNo, true);
		UserActDO userAct = userActMysqlDAO.findByActNoLock(actNo);
		ValidateUtil.notNull(userAct, actNo+"账户不存在");
		
		ActState actState = ActState.valueOf(userAct.getActState());
		if (actState == null) {
			LOG.error("账户"+actNo+"状态异常,actState="+userAct.getActState());
			throw new BusinessException("账户"+actNo+"状态异常");
		}
		
		if (actState == ActState.CANCELLATION) {
			LOG.info("账户"+actNo+"已注销");
			return;
		}
		
		recordActFreezeFlow(userAct, operatorUser, FreezedOperator.CLOSE);
		UserActDO updateUserAct = new UserActDO();
		updateUserAct.setActState(ActState.CANCELLATION.getCode());		
		updateUserAct.setUpdateUser(operatorUser);
		updateUserAct.setModifyTime(new Date());
		
		if (StringUtil.isNotBlank(attach)) {
			updateUserAct.setInnerRemark(attach);
		}
		
		userActMysqlDAO.updateAct(updateUserAct, userAct.getActNo());
	}

	@Override
	@Transactional
	public void freezeAct(String actNo, String operatorUser, String attach) {	
		ValidateUtil.isNotBlank(operatorUser, "operatorUser不能为空");
		
		UserActDO userAct = getUserAct(actNo, true, false);		
		ActState actState = ActState.valueOf(userAct.getActState());
		
		if (actState == ActState.STOP_PAY) {
			LOG.info("账户"+actNo+"已冻结");
			return;
		}
		
		if (actState == ActState.NORMAL) {
			recordActFreezeFlow(userAct, operatorUser, FreezedOperator.FREEZE);
			UserActDO updateUserAct = new UserActDO();
			updateUserAct.setActState(ActState.STOP_PAY.getCode());
			updateUserAct.setUpdateUser(operatorUser);
			updateUserAct.setModifyTime(new Date());
			
			if (StringUtil.isNotBlank(attach)) {
				updateUserAct.setInnerRemark(attach);
			}
			
			userActMysqlDAO.updateAct(updateUserAct, userAct.getActNo());
		}
	}

	@Override
	@Transactional
	public void unfreezeAct(String actNo, String operatorUser, String attach) {		
		ValidateUtil.isNotBlank(operatorUser, "operatorUser不能为空");
		
		UserActDO userAct = getUserAct(actNo, true, false);		
		ActState actState = ActState.valueOf(userAct.getActState());
		
		if (actState == ActState.NORMAL) {
			LOG.info("账户"+actNo+"已解冻");
			return;
		}
		
		if (actState == ActState.STOP_PAY) {
			recordActFreezeFlow(userAct, operatorUser, FreezedOperator.UNFREEZE);
			UserActDO updateUserAct = new UserActDO();
			updateUserAct.setActState(ActState.NORMAL.getCode());
			updateUserAct.setUpdateUser(operatorUser);
			updateUserAct.setModifyTime(new Date());

			if (StringUtil.isNotBlank(attach)) {
				updateUserAct.setInnerRemark(attach);
			}
			userActMysqlDAO.updateAct(updateUserAct, userAct.getActNo());
		}
	}

	@Override
	@Transactional
	public Date plusMoney(String actNo, long amount, String originOrderNo,
            String payFlowId, Date payTime, TradeOrderType tradeType,  String tradeMsg, String businessType) {
		
		isValidAmount(amount);
		
		UserActDO userAct = getUserAct(actNo, true, false);
		
		isBalanceOverFlow(userAct, amount);
	
		recordBalanceChangeFlow(userAct, null, originOrderNo, payFlowId, amount, payTime, tradeMsg, tradeType, businessType);
		
		userActMysqlDAO.updateActBalance(actNo, amount);
		
		return new Date();
	}
	
	
	@Override
	@Transactional
	public Date subMoney(String actNo, long amount, String originOrderNo,
            String payFlowId, Date payTime, TradeOrderType tradeType,  String tradeMsg, String businessType) {
		
		isValidAmount(amount);
		
		boolean isPayer = true;
		//加款收续费可以出钱
		if (tradeType != null && TradeOrderType.TRADE_FEE == tradeType) {
			isPayer = false;
		}
		UserActDO userAct = getUserAct(actNo, true, isPayer);
		
		isBalanceOverFlow(userAct, -amount);
		
		isBalanceSufficient(userAct, amount);

		recordBalanceChangeFlow(userAct, null, originOrderNo, payFlowId, -amount, payTime, tradeMsg, tradeType, businessType);
		
		userActMysqlDAO.updateActBalance(actNo, -amount);
		
		return new Date();	
	}

	
	@Override
	@Transactional
	public Date freezeMoney(String actNo, long amount, String originOrderNo,
            String payFlowId, Date payTime, TradeOrderType tradeType,  String tradeMsg, String updateUser, String businessType) {
		
		isValidAmount(amount);
		
		UserActDO userAct = getUserAct(actNo, true, false);
		
		isBalanceSufficient(userAct, amount);
		
		recordBalanceFreezeFlow(userAct, originOrderNo, payFlowId, amount, payTime, tradeMsg, tradeType, updateUser, businessType);
		
		userActMysqlDAO.updateActFreezeBalance(actNo, amount);
		
		return null;
	}

	@Override
	@Transactional
	public Date unfreezeMoney(String actNo, long amount, String originOrderNo,
            String payFlowId, Date payTime, TradeOrderType tradeType,  String tradeMsg, String updateUser, String businessType) {

		isValidAmount(amount);
		
		UserActDO userAct = getUserAct(actNo, true, false);
		
		long freezeBalance = userAct.getFreezeBalance();
		if (amount > freezeBalance) {
			LOG.warn("账户"+userAct.getActNo()+"冻结金额："+freezeBalance+",解冻金额："+amount);
			throw new BusinessException("解冻金额不能大于冻结金额");
		}
		
		recordBalanceFreezeFlow(userAct, originOrderNo, payFlowId, -amount, payTime, tradeMsg, tradeType, updateUser, businessType);
		
		userActMysqlDAO.updateActFreezeBalance(actNo, -amount);
		
		return null;
	}

	@Override
	@Transactional
	public Date transferAct(String srcActNo, String desActNo, long amount, String originOrderNo,
            String payFlowId, TradeOrderType tradeType,  String tradeMsg, String businessType) {
		
		    Date payTime = new Date();
		
			isValidAmount(amount);
			
			boolean isPayer = true;
			//加款收续费可以出钱
			if (tradeType != null && TradeOrderType.TRADE_FEE == tradeType) {
				isPayer = false;
			}
			
			UserActDO srcAct = getUserAct(srcActNo, true, isPayer);
			
			isBalanceOverFlow(srcAct, -amount);
			
			isBalanceSufficient(srcAct, amount);
		
			UserActDO desAct = getUserAct(desActNo, true, false);
			
			ValidateUtil.isTrue(!srcAct.equals(desAct), "转账账号相同");
				
			recordBalanceChangeFlow(srcAct, desAct, originOrderNo, payFlowId, -amount, payTime, tradeMsg, tradeType, businessType);
			
			userActMysqlDAO.updateActBalance(srcActNo, -amount);
						
			recordBalanceChangeFlow(desAct, srcAct, originOrderNo, payFlowId, amount, payTime, tradeMsg, tradeType, businessType);
			
			userActMysqlDAO.updateActBalance(desActNo, amount);
		
		return payTime;
	}
	
	/**
	 * 根据账户号获取账户信息
	 * 
	 * @param actNo    账户号
	 * @param isLock   是否锁定账户信息
	 * @param isPayer  是否为付款方
	 * @return
	 * @author xiaosc
	 * @date 2016年7月19日 下午4:56:54 
	 */
	private UserActDO getUserAct(String actNo, boolean isLock, boolean isPayer){		
		ValidateUtil.isNotBlank(actNo, "actNo不能为空");
		
//		UserActDO userAct = userActMysqlDAO.findByActNo(actNo, true);
		UserActDO userAct = userActMysqlDAO.findByActNoLock(actNo);
		ValidateUtil.notNull(userAct, actNo+"账户不存在");
		
		ActState actState = ActState.valueOf(userAct.getActState());
		if (actState == null) {
			LOG.error("账户"+actNo+"状态异常,actState="+userAct.getActState());
			throw new BusinessException("账户"+actNo+"状态异常");
		}
		
		if (actState == ActState.CANCELLATION) {
			throw new BusinessException("账户"+actNo+"已注销");
		}
		
		if (isPayer && actState == ActState.STOP_PAY) {
			throw new BusinessException("账户"+actNo+"已冻结");
		}
		
		return userAct;
	}
	
	
	private void isValidAmount(long amount){
		if (amount <= 0) {
			throw new BusinessException("交易金额不能小于0");
		}
	}
	
	/**
	 * 判断账户余额是否足够
	 * <功能详细描述>
	 * @author xiaosc
	 * @date 2016年7月19日 下午4:52:45 
	 */
	private void isBalanceSufficient(UserActDO userAct, long amount){		
		long balance = userAct.getActBalance();	             //用户余额
		long freezeBalance = userAct.getFreezeBalance();     //冻结金额
		
		if (balance - freezeBalance < amount) {
			LOG.warn("banlance of act "+userAct.getActNo()+" insufficient,balance:"+balance+" freeze_balance:"+freezeBalance+" amount:"+amount);
			throw new BusinessException("账户"+userAct.getActNo()+"余额不足");
		}
	}
	
	/**
	 * 
	 * <功能详细描述>
	 * @param userAct
	 * @param amount
	 * @author xiaosc
	 * @date 2016年7月19日 下午5:16:12 
	 */
	private void isBalanceOverFlow(UserActDO userAct, long amount) {
		if (userAct.getActBalance() + amount < 0){
			LOG.warn("balance of act "+userAct.getActNo()+" over flow,balance:"+userAct.getActBalance() + ",amount:"+amount);
			throw new BusinessException("账户"+userAct.getActNo()+"余额不足");
		}
	}
	
	
	// 写账户流水
	private void recordBalanceChangeFlow(UserActDO userAct, UserActDO toUserAct, String originOrderNo,
			String payFlowId, long amount, Date payTime, String tradeMsg,
			TradeOrderType tradeType, String businessType) {

		UserActFlowDO actFlowDO = new UserActFlowDO();
		String flowNo = billnoService.nextUserAccountFlow();
		Date cDate = new Date();
		
		if (payTime == null) {
			payTime = cDate;
		}

		actFlowDO.setActFlowNo(flowNo);
		actFlowDO.setActNo(userAct.getActNo());
		actFlowDO.setActType(userAct.getActType());
		actFlowDO.setOriginOrderNo(originOrderNo);
		actFlowDO.setTradeOrderNo(payFlowId);
		actFlowDO.setInnerRemark(tradeType.getDesc());
		actFlowDO.setTradeTime(payTime);
		actFlowDO.setTradeType(tradeType.getCode());
		actFlowDO.setUserId(userAct.getUserId());
		actFlowDO.setUserRemark(tradeMsg);
		actFlowDO.setCreateTime(cDate);
		actFlowDO.setModifyTime(cDate);
		actFlowDO.setUpdateUser("admin");
		actFlowDO.setTranAmount(amount);
		actFlowDO.setFreezeAmount(0l);
		actFlowDO.setBusinessType(businessType);
		if (null != toUserAct){
			actFlowDO.setToActNo(toUserAct.getActNo());
			actFlowDO.setToActType(toUserAct.getActType());
			actFlowDO.setToUserId(toUserAct.getUserId());
		}
				
		StringBuffer sb = new StringBuffer(payFlowId);		
		sb.append(userAct.getActNo());
		sb.append(userAct.getUserType());
		sb.append(amount); //交易经额 
		sb.append(tradeType.getCode());
		sb.append(Constance.ACT_SIGIN);
		//同一笔支付流水订单，同一个账户同样金额只能加减款一次
		
		String actSign = MD5Util.encrypt(sb.toString());
		actFlowDO.setActSign(actSign );

		userActFlowMysqlDAO.saveBalanceFlow(actFlowDO);
	}
	
	private void recordBalanceFreezeFlow(UserActDO userAct, String originOrderNo,
			String payFlowId, long freezeBalance, Date payTime, String tradeMsg,
			TradeOrderType tradeType, String updateUser, String businessType) {

		UserActFlowDO actFlowDO = new UserActFlowDO();
		String flowNo = billnoService.nextUserAccountFlow();
		Date cDate = new Date();
		
		if (payTime == null) {
			payTime = cDate;
		}

		actFlowDO.setActFlowNo(flowNo);
		actFlowDO.setActNo(userAct.getActNo());
		actFlowDO.setActType(userAct.getActType());
		actFlowDO.setOriginOrderNo(originOrderNo);
		actFlowDO.setTradeOrderNo(payFlowId);
		actFlowDO.setInnerRemark(tradeType.getDesc());
		actFlowDO.setTradeTime(payTime);
		actFlowDO.setTradeType(tradeType.getCode());
		actFlowDO.setUserId(userAct.getUserId());
		actFlowDO.setUserRemark(tradeMsg);
		actFlowDO.setCreateTime(cDate);
		actFlowDO.setModifyTime(cDate);
		actFlowDO.setTranAmount(0l);
		actFlowDO.setUpdateUser(updateUser);
		actFlowDO.setFreezeAmount(freezeBalance);	
		actFlowDO.setBusinessType(businessType);
		
		StringBuffer sb = new StringBuffer(flowNo);		
		sb.append(userAct.getActNo());
		sb.append(userAct.getUserType());
		sb.append(freezeBalance);
		sb.append(Constance.ACT_SIGIN);
				
		String actSign = MD5Util.encrypt(sb.toString());
		actFlowDO.setActSign(actSign );
		
		userActFlowMysqlDAO.saveFreezeFlow(actFlowDO);
	}
	
	private void recordActFreezeFlow(UserActDO userAct, String operatorUser, FreezedOperator operatorType){
		
		Date cDate = new Date();		
		UserActFreezeDO userActFreezeDO = new UserActFreezeDO();		
		userActFreezeDO.setActNo(userAct.getActNo());
		userActFreezeDO.setAmount(userAct.getActBalance());
		userActFreezeDO.setCreateTime(cDate);
		userActFreezeDO.setModifyTime(cDate);
		userActFreezeDO.setOperator(operatorUser);
		userActFreezeDO.setOperaType(operatorType.getCode());
		userActFreezeDO.setUserId(userAct.getUserId());		
		userActFreezeMysqlDAO.save(userActFreezeDO);
		
		
	}

	@Override
	@Transactional
	public List<BatchActTransferRsp> bacthTransferActs(List<BatchActTransferReq> list) {		
		
		ValidateUtil.notEmpty(list, "请求参数不能为空");	
		List<BatchActTransferRsp> rspList = new ArrayList<BatchActTransferRsp>();
		
		for (BatchActTransferReq req : list) {			
			Date payTime = transferAct(req.getSrcActNo(), req.getDesActNo(), req.getAmount(), req.getOriginOrderNo(), req.getPayFlowId(), req.getTradeType(), req.getTradeMsg(), req.getBusinessType());
			
			rspList.add(new BatchActTransferRsp(req.getOriginOrderNo(), req.getPayFlowId(), payTime));
		}
		
		return rspList;
	}
	
	
	private UserActDO initUserAct(String userId, UserActType userActType, String innerRemark, String userRemark){
		String actNo = billnoService.nextNo(userActType.getTemplateId());		
		Date cDate = new Date();
		
		UserActDO userAct = new UserActDO();
		userAct.setUserId(userId);
//		userAct.setUserType(userType);   
//		userAct.setActLevel(actLevel);
		userAct.setActType(userActType.getCode());
		userAct.setActBalance(0l);		
		userAct.setActNo(actNo);
		userAct.setActState(1);
		userAct.setBalanceTime(cDate);
		userAct.setChannelId("");
		userAct.setCreateTime(cDate);
		userAct.setFreezeBalance(0l);
		userAct.setInnerRemark(innerRemark);
		userAct.setModifyCount(0);
		userAct.setModifyTime(cDate);
		userAct.setUpdateUser("admin");
		userAct.setUserRemark(userRemark);
		
		return userAct;
	}

	@Override
	public List<UserActDO> findByUserId(String userId) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
	
		return userActMysqlDAO.listByUserId(userId);		
	}

	@Override
	public List<UserActDO> findByActNoList(List<String> actNoList) {
		ValidateUtil.notEmpty(actNoList, "actNoList不能为空");
		List<FilterRule> filterRules = new ArrayList<>();
		filterRules.add(new FilterRule("act_no", "in", actNoList));
		
		return userActMysqlDAO.findByPage(filterRules , null);		
	}

	@Override
	public UserActDO findUserAct(String userId, UserActType userActType) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(userActType, "userActType不能为空");
		
		return userActMysqlDAO.findByUserId(userId, userActType.getCode());	
	}
	
	@Override
	public List<UserActDO> findUsersAct(List<String> userList, UserActType userActType){
		ValidateUtil.notNull(userList, "userList不能为空");
		ValidateUtil.notNull(userActType, "userActType不能为空");
		return userActMysqlDAO.findByUsersId(userList, userActType.getCode());
	}

	@Override
	public UserActDO findUserAct(String actNo) {
		
		ValidateUtil.isNotBlank(actNo, "actNo不能为空");		
		return userActMysqlDAO.findByActNo(actNo, false);
	}

	@Override
	public List<UserActDO> findByUserList(List<String> userList) {
		ValidateUtil.notEmpty(userList, "userList不能为空");
		List<FilterRule> filterRules = new ArrayList<>();
		filterRules.add(new FilterRule("user_id", "in", userList));
		
		return userActMysqlDAO.findByPage(filterRules , null);
	}

	@Override
	public void updateUserAct(String userId, String remark, int actState,
			String updateUser, int actType) {
		UserActDO userActDO = userActMysqlDAO.findByUserId(userId, actType);
		if(userActDO == null){
			throw new BusinessException("未查询到帐号"+userId+"的对应的账户类型");
		}
		UserActDO upUserActDO = new UserActDO();
		upUserActDO.setUserRemark(remark);
		upUserActDO.setActState(actState);
		upUserActDO.setUpdateUser(updateUser);
		userActMysqlDAO.updateAct(upUserActDO, userActDO.getActNo());
		
	}

	@Override
	public UserActDO findActInfo(String userId, UserActType userActType) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(userActType, "userActType不能为空");
		
		return userActMysqlDAO.findActInfo(userId, userActType.getCode());
	}

	@Override
	public UserActDO findValidActInfo(String userId, UserActType userActType, boolean isPayer) {
		UserActDO userAct = findActInfo(userId, userActType);
		if (userAct == null) {
			throw new BusinessException("用户"+userId+userActType.getDesc()+"不存在");
		}
		
		Integer actState = userAct.getActState();
		if (actState == null) {
			LOG.error("用户{}{}状态为{}异常",userId, userActType.getDesc(), actState);
			throw new BusinessException("用户"+userId+userActType.getDesc()+"状态异常");
		}
		
		ActState state = ActState.valueOf(actState);
		if (state == null) {
			LOG.error("用户{}{}状态为{}非法",userId, userActType.getDesc(), actState);
			throw new BusinessException("用户"+userId+userActType.getDesc()+"状态非法");
		}else if (state == ActState.CANCELLATION) {
			throw new BusinessException("用户"+userId+userActType.getDesc()+"已注销");
		}else if (isPayer && state == ActState.STOP_PAY) {
			throw new BusinessException("用户"+userId+userActType.getDesc()+"已冻结");
		}
		
		return userAct;
	}

}
